/*
 * Copyright 2012 Gregor Schauer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.schauer.gregor.vienna.metadata;

import org.apache.commons.lang3.ArrayUtils;
import org.hibernate.SessionFactory;
import org.hibernate.internal.SessionImpl;
import org.hibernate.metadata.ClassMetadata;
import org.springframework.beans.BeanUtils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.io.Serializable;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Member;
import java.lang.reflect.Method;

/**
 * @author Gregor Schauer
 */
public class HibernateMetadataProvider<T> implements MetadataProvider<T> {
	protected SessionFactory sessionFactory;

	public HibernateMetadataProvider() {
	}

	public HibernateMetadataProvider(@Nonnull SessionFactory sessionFactory) {
		setSessionFactory(sessionFactory);
	}

	@Nullable
	@Override
	public Object getIdentifier(@Nonnull T obj) {
		ClassMetadata metadata = sessionFactory.getClassMetadata(obj.getClass());
		return metadata.getIdentifier(obj, getSession());
	}

	@Override
	public void setIdentifier(@Nonnull T obj, @Nullable Object identifier) {
		ClassMetadata metadata = sessionFactory.getClassMetadata(obj.getClass());
		metadata.setIdentifier(obj, (Serializable) identifier, getSession());
	}

	@Nonnull
	@Override
	public String getIdentifierName(@Nonnull Class<? extends T> clazz) {
		ClassMetadata metadata = sessionFactory.getClassMetadata(clazz);
		return metadata.getIdentifierPropertyName();
	}

	@Override
	public boolean isTransientType(@Nonnull Class<? extends T> clazz) {
		return false;
	}

	@Override
	public <M extends AccessibleObject & Member> boolean isTransient(@Nonnull M member) {
		ClassMetadata metadata = sessionFactory.getClassMetadata(member.getDeclaringClass());
		String name = member instanceof Method ? BeanUtils.findPropertyForMethod((Method) member).getName() : member.getName();
		return !ArrayUtils.contains(metadata.getPropertyNames(), name) && !name.equals(metadata.getIdentifierPropertyName());
	}

	@Nonnull
	@Override
	public String getEntityName(@Nonnull Class<? extends T> clazz) {
		ClassMetadata metadata = sessionFactory.getClassMetadata(clazz);
		return metadata.getEntityName();
	}

	@Inject
	public void setSessionFactory(@Nonnull SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Nonnull
	protected SessionImpl getSession() {
		return SessionImpl.class.cast(sessionFactory.getCurrentSession());
	}
}
